package card_discount

import (
	"bp/internal/render"
	"bp/internal/service/param"
	"bp/internal/util"
	cardDiscountGrpc "bp/rpc/gen/webapi/card_discount"
	"context"
)

// GetCardDiscount 获取卡折扣
func (s *CardDiscountWebApiImp) GetCardDiscount(ctx context.Context, req *cardDiscountGrpc.GetCardDiscountReq) (res *cardDiscountGrpc.GetCardDiscountRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/query.GetCardDiscount=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.GetCardDiscountRes{}
	q := s.dao.Q

	cardDiscountID, err := s.VldGetCardDiscountReq(ctx, q, req)
	if err != nil {
		return
	}
	cardDiscountPtr, err := s.entityHub.CardDiscountService.GetCardDiscountPtr(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	itemCategoryConfPtrs, err := s.entityHub.CardDiscountService.ListItemCategoryConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	itemConfPtrs, err := s.entityHub.CardDiscountService.ListItemConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	res.CardDiscount = s.ConvCardDiscountPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)(cardDiscountPtr)
	return
}

// GetCardDiscountByOutCardID 根据外部卡id获取卡折扣
func (s *CardDiscountWebApiImp) GetCardDiscountByOutCardID(ctx context.Context, req *cardDiscountGrpc.GetCardDiscountByOutCardIDReq) (res *cardDiscountGrpc.GetCardDiscountByOutCardIDRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/query.GetCardDiscountByOutCardID=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &cardDiscountGrpc.GetCardDiscountByOutCardIDRes{}
	q := s.dao.Q

	outCardID, err := s.VldGetCardDiscountByOutCardIDReq(ctx, q, req)
	if err != nil {
		return
	}
	cardDiscountPtr, err := s.entityHub.CardDiscountService.GetCardDiscountPtrByOutCardID(ctx, q, outCardID)
	if err != nil {
		return
	}
	cardDiscountID := cardDiscountPtr.ID
	itemCategoryConfPtrs, err := s.entityHub.CardDiscountService.ListItemCategoryConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	itemConfPtrs, err := s.entityHub.CardDiscountService.ListItemConfigurationPtrsByCardDiscountID(ctx, q, cardDiscountID)
	if err != nil {
		return
	}
	res.CardDiscount = s.ConvCardDiscountPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)(cardDiscountPtr)
	return
}

// ListCardDiscounts 列出卡折扣
func (s *CardDiscountWebApiImp) ListCardDiscounts(ctx context.Context, req *cardDiscountGrpc.ListCardDiscountsReq) (res *cardDiscountGrpc.ListCardDiscountsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/query.ListCardDiscounts=>%+v", req)
	res = &cardDiscountGrpc.ListCardDiscountsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListCardDiscountsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, cardDiscountPtrs, err := s.entityHub.CardDiscountService.ListCardDiscountPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}
	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.CardDiscountService.GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByCardDiscountPtrs(ctx, q, cardDiscountPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.CardDiscountService.GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountPtrs(ctx, q, cardDiscountPtrs)
		if err != nil {
			return
		}
	}
	res.Total = int32(total)
	res.Rows = s.ConvCardDiscountPtrArrayEntityToPb(itemCategoryMap, itemMap)(cardDiscountPtrs)
	return
}

// ListCardDiscountWithStats 列出卡折扣及其统计
func (s *CardDiscountWebApiImp) ListCardDiscountWithStats(ctx context.Context, req *cardDiscountGrpc.ListCardDiscountWithStatsReq) (res *cardDiscountGrpc.ListCardDiscountWithStatsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("card_discount_imp/query.ListCardDiscountWithStats=>%+v", req)
	res = &cardDiscountGrpc.ListCardDiscountWithStatsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListCardDiscountWithStatsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, cardDiscountWithStatPtrs, err := s.entityHub.CardDiscountService.ListCardDiscountWithStatPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}
	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.CardDiscountService.GetMapOfCardDiscountIDToItemCategoryConfigurationPtrsByListCardDiscountWithStatRowPtrs(ctx, q, cardDiscountWithStatPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.CardDiscountService.GetMapOfCardDiscountIDToItemConfigurationPtrsByListCardDiscountWithStatRowPtrs(ctx, q, cardDiscountWithStatPtrs)
		if err != nil {
			return
		}
	}
	res.Total = int32(total)
	res.Rows = s.ConvCardDiscountWithStatPtrArrayEntityToPb(itemCategoryMap, itemMap)(cardDiscountWithStatPtrs)
	return
}
